package com.mall.market.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.market.mapper.SmsHomeRecommendProductMapper;
import com.mall.model.domain.SmsHomeRecommendProduct;
import com.mall.model.dto.SmsHomeRecommendProductDto;
import com.mall.model.vo.CommonPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SmsHomeRecommendProductService extends ServiceImpl<SmsHomeRecommendProductMapper, SmsHomeRecommendProduct> {
    @Autowired(required = false)
    private SmsHomeRecommendProductMapper smsHomeRecommendProductMapper;


    /**
     * 分页查询推荐
     *
     * @param dto
     * @return
     */
    public CommonPage list(SmsHomeRecommendProductDto dto) {
        Page<SmsHomeRecommendProduct> pageInfo = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<SmsHomeRecommendProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(dto.getProductName() != null, SmsHomeRecommendProduct::getProductName, dto.getProductName());
        queryWrapper.eq(dto.getRecommendStatus() != null, SmsHomeRecommendProduct::getRecommendStatus, dto.getRecommendStatus());
        queryWrapper.orderByDesc(SmsHomeRecommendProduct::getSort);
        Integer count = smsHomeRecommendProductMapper.selectCount(queryWrapper);
        smsHomeRecommendProductMapper.selectPage(pageInfo, queryWrapper);
        return new CommonPage(dto.getPageNum(), dto.getPageSize(), count, pageInfo.getRecords());
    }

    //增
    public void create(List<SmsHomeRecommendProduct> smsHomeRecommendProductList) {
        
        this.saveBatch(smsHomeRecommendProductList);
    }

    //删
    public void delete(List<Integer> ids) {
        LambdaQueryWrapper<SmsHomeRecommendProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeRecommendProduct::getId, ids);
        smsHomeRecommendProductMapper.delete(wrapper);
    }

    //修改状态
    public void updateStatus(List<Integer> ids, Integer recommendStatus) {
        LambdaQueryWrapper<SmsHomeRecommendProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SmsHomeRecommendProduct::getId, ids);
        List<SmsHomeRecommendProduct> smsHomeRecommendProducts = smsHomeRecommendProductMapper.selectList(wrapper);
        for (SmsHomeRecommendProduct smsHomeRecommendProduct : smsHomeRecommendProducts) {
            if (smsHomeRecommendProduct != null) {
                smsHomeRecommendProduct.setRecommendStatus(recommendStatus);
                smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
            }
        }
    }

    //修改排序
    public void updateSort(Integer id, Integer sort) {
        SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductMapper.selectById(id);
        if (smsHomeRecommendProduct != null) {
            smsHomeRecommendProduct.setSort(sort);
            smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
        }
    }


}
